﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;


namespace iLangl.Model
{
	public class SyncedDataResponseContext : BaseResponseContext
	{
		SyncedOneWayData request = null;
		public override void SetContext(object context)
		{
			base.SetContext(context);

			if (document == null)
				return;

			request = new SyncedOneWayData();

			XmlNodeList nodes = document.SelectNodes("//Companies/Company");

			CompanyResponseContext companyResponseContext = new CompanyResponseContext();
			
			IList<Company> companies = new List<Company> ();

			foreach (XmlNode company in nodes)
			{
				companyResponseContext.SetContext(company);
				companies.Add((Company)companyResponseContext.GetContext());
			}

			IList<Project> projects = new List<Project>();

			nodes = document.SelectNodes("//Projects/Project");
			ProjectResponseContext projectResponseContext = new ProjectResponseContext();
			foreach (XmlNode project in nodes)
			{
				projectResponseContext.SetContext(project);
				projects.Add((Project)projectResponseContext.GetContext());
			}


			nodes = document.SelectNodes("//Alerts/Alert");

			IList<ISerializedAlert> alerts = new List<ISerializedAlert>();
			AlertDesirializeContext alertdesirialization = new AlertDesirializeContext();

			foreach (XmlNode alert in nodes)
			{
				alertdesirialization.SetContext(alert);

				alerts.Add( (ISerializedAlert) alertdesirialization.GetContext());
			}


			request.Alerts = alerts.ToArray();
			request.Companies = companies.ToArray();
			request.Projects = projects.ToArray();
		}

		public override object GetContext()
		{
			return request;
		}
	}
}
